package org.wattdepot.cli.command;

import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import org.wattdepot.client.BadXmlException;
import org.wattdepot.client.MiscClientException;
import org.wattdepot.client.NotAuthorizedException;
import org.wattdepot.client.ResourceNotFoundException;
import org.wattdepot.resource.property.jaxb.Property;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.resource.source.summary.jaxb.SourceSummary;

/**
 * Prints information about a single source.
 * 
 * @author Alex Kan and Kelvin Green
 * @version 2.0
 */
public class WattDepotListSummaryCommand extends WattDepotCommand {
  /**
   * Prints information about a single source.
   * 
   * @param args contains an array of strings created from the user input.
   * @return a String containing summary of Source.
   */
  public String doCommand(String args[]) {
    if (args.length < 2 || args.length > 2) {
      return "Correct command is summary {source}";
    }
    return getSummary(args);
  }
  
  /**
   * Gets information about a single Source.
   * 
   * @param args contains an array of strings created from the user input.
   * @return a string variable named output containing all the output for the method.
   */
  private String getSummary(String args[]) {
 // Holds temporary output data during processing.
    ArrayList<String> outputArray = new ArrayList<String>();
    String output = ""; // Holds the output that will be returned at the end of the method.
    boolean error = false; // Is set to true when an error occurs.
    Source source = null; // Holds the source returned from the wattdepot client service.
    try {
      source = getClient().getSource(args[1]);
      output += "Subsources: ";
      ArrayList<String> ssList = (ArrayList<String>)getSubSources(source);
      ListIterator<String> li = ssList.listIterator();
      while (li.hasNext()) {
        outputArray.add(li.next());
      }
      output += outputArray.toString();
      output += cr;
      output += "Description: " + source.getDescription() + cr;
      output += "Owner: ";
      String[] sOwner = source.getOwner().split("/");
      output += sOwner[sOwner.length - 1] + cr;
      output += "Location: " + source.getLocation() + cr;
      output += "Coordinates: " + source.getCoordinates() + cr;
      output += "Properties: ";
      ArrayList<String> propertiesList = new ArrayList<String>();
      outputArray = new ArrayList<String>();

      org.wattdepot.resource.property.jaxb.Properties props = source.getProperties();
      List<Property> propList = null;
      try {
        propList = props.getProperty();

        ListIterator<Property> propIterator = propList.listIterator();
        while (propIterator.hasNext()) {
          Property prop = propIterator.next();
          outputArray.add("(" + prop.getKey() + " : " + prop.getValue() + ")");
        }
      }
      catch (Exception e) {
        error = true;
      }
      output += outputArray.toString();

      outputArray = new ArrayList<String>();
      ListIterator<String> lit = propertiesList.listIterator();
      while (lit.hasNext()) {
        outputArray.add(lit.next());
      }
      output += outputArray.toString() + cr;
      output = output.replace(",[", "");
      output = output.replace("]", " ");
      output = output.replace("[", " ");
    }
    catch (NotAuthorizedException e1) {
      error = true;
    }
    catch (ResourceNotFoundException e1) {
      error = true;
      if (error) {
        return RES_NOT_FOUND;
      }
    }
    catch (BadXmlException e1) {
      error = true;
    }
    catch (MiscClientException e1) {
      return CON_NOT_FOUND;
    }
    SourceSummary srcSummary = null;
    try {
      srcSummary = getClient().getSourceSummary(args[1]);

      output += "Earliest data: ";

      output += srcSummary.getFirstSensorData().toString();

      output += cr;
      output += "Latest data: ";

      output += srcSummary.getLastSensorData().toString();

      output += cr;
      output += "Total data points: ";

      output += String.valueOf(srcSummary.getTotalSensorDatas());

      output += cr;
    }
    catch (NotAuthorizedException e) {
      error = true;
      return "";
    }
    catch (ResourceNotFoundException e) {
      error = true;
      if (error) {
        return RES_NOT_FOUND;
      }
    }
    catch (BadXmlException e) {
      error = true;
      return "";
    }
    catch (MiscClientException e) {
      return CON_NOT_FOUND;
    }

    return output;
  }
}